/**
 * 全局路由拦截
 */
import Vue from 'vue'

export default function ({ app, store }) {
  /**
   * 前置拦截
   *
   */
  app.router.beforeEach((to, from, next) => {
    if (to.fullPath === '/zh/solution' || to.fullPath === '/solution') {
      return
    }
    next()
  })

  /**
   * 后置拦截
   *
   */
  app.router.afterEach((to, from, next) => {
    viewPageTrack(to.path, to.query)
  })
}

// 页面浏览埋点
function viewPageTrack(currentPath, currentQuery) {
  if (!currentPath) {
    return
  }

  const { id, type } = currentQuery
  let queryType = id ? 1 : ''
  if (type) {
    queryType = type
  }
  const trackParams = {
    pagePath: currentPath,
    query: JSON.stringify(currentQuery),
    type: queryType, // 类型 1：解决方案，2：产品，3：新闻
    bizId: id ?? '', // 业务id, 产品和文章的id
  }
  Vue.prototype.$track(trackParams)
}
